﻿import flash.geom.Point;
import flash.geom.Rectangle;
import mx.utils.Delegate;

import kliment.controls.ControlsAbstract;
import kliment.utils.TransformCalc;
import kliment.display.MCC;
import kliment.geom.BoundsRectangle;
import kliment.geom.SimplePoint;
import kliment.controls.Orientation;

/**
 * ...
 * @author	Kliment
 * @version 1.2.3
 */
class kliment.effect.ZPanoram extends ControlsAbstract {
	private var _space_mc:MovieClip;
	private var _elements:Array;
	private var _ratio:Number;
	private var _orientation:Object;
	private var _enableHorizontal:Boolean;
	private var _enableVertical:Boolean;
	
	public function ZPanoram(space_mc:MovieClip, elements:Array, ratio:Number, orientation:Object) {
		_space_mc = space_mc;
		_elements = elements || new Array();
		_ratio = ratio || .2;
		_orientation = orientation || Orientation.HORIZONTAL;
		_init();
	}
	
	private function _init():Void {
		_setOrientation();
		enable = true;
	}
	
	public function set enable(value:Boolean):Void {
		if (value)
			_space_mc.onEnterFrame = Delegate.create(this, _move);
		else 
			_space_mc.onEnterFrame = null;
		super.enable = value;
	}
	
	public function get orientation():Object { return _orientation }
	public function set orientation(value:Object):Void {
		_orientation = value;
		_setOrientation();
	}
	
	private function _setOrientation():Void {
		if (_orientation == Orientation.HORIZONTAL || _orientation == Orientation.FULL)
			_enableHorizontal = true;
		if (_orientation == Orientation.VERTICAL || _orientation == Orientation.FULL)
			_enableVertical = true;
	}
	
	private function _move():Void {
		var bounds:BoundsRectangle = MCC.getBounds(_space_mc);
		var space:Rectangle = bounds.toRectangle();
		var inSpace:Boolean = bounds.includePoint(new SimplePoint(_space_mc._parent._xmouse, _space_mc._parent._ymouse));
		var enableHorizontal:Boolean
		var i:Number = _elements.length;
		var item:MovieClip;
		while (i--) {
			item = _elements[i];	
			
			if (inSpace) {
				if (_enableHorizontal)
					item.positionX = -((item._parent._xmouse - space.x) / space.width * (item._width - space.width)) + space.x;
				if (_enableVertical)
					item.positionY = -((item._parent._ymouse - space.y) / space.height * (item._height- space.height)) + space.y;
			}
			
			var testPoint:Point = TransformCalc.fixInSpace(new Rectangle(item.positionX || item._x, item.positionY || item._y, item._width, item._height), space);			
			
			if (_enableHorizontal) {
				if (item.positionX != testPoint.x) {
					item.positionX = testPoint.x;
					item._x = item.positionX;
				} else 
					item._x += (item.positionX - item._x) * _ratio;
			}
			if (_enableVertical) {
				if (item.positionY != testPoint.y) {
					item.positionY = testPoint.y;
					item._y = item.positionY;
				} else 
					item._y += (item.positionY - item._y) * _ratio;
			}
		}
	}
	
	public function remove():Void {
		enable = false;
		delete _elements;
		delete this;
	}
}